<template>
  <div>
    <el-header class="the-header">
      <div class="the-header-left">
        <div class="the-header-left-logo-title">
          <img src="/web/favicon.ico">
          <span class="the-header-left-logo-title-span"  style="margin-left: 10px">Wzzzy</span>
        </div>
        <el-menu
          :default-active="activeIndex"
          class="the-header-el-menu"
          mode="horizontal"
          :router="true">
          <el-menu-item index="/index">
            主页
          </el-menu-item>
          <el-menu-item index="/free">体系课</el-menu-item>
          <el-menu-item index="/charge">
            实战课
          </el-menu-item>
          <el-submenu index="5">
            <template slot="title">更多</template>
            <el-menu-item index="/about">关于我</el-menu-item>
            <el-menu-item index="/contact">联系方式</el-menu-item>
            <el-menu-item index="/more">更多信息</el-menu-item>
          </el-submenu>
        </el-menu>
      </div>
      <div class="the-header-right">
        <div class="the-header-right-div" type="text" v-show="loginMember.id" :underline="false">{{loginMember.name}}&nbsp;&nbsp;|</div>
        <el-button class="the-header-right-button" type="text" v-show="loginMember.id" @click="logout()">&nbsp;&nbsp;退出登录</el-button>
        <el-button class="the-header-right-button" type="text" v-show="!loginMember.id" @click="openLoginDialog()">登录  /  注册</el-button>
      </div>
    </el-header>
    <the-login-dialog ref="loginComponent"></the-login-dialog>
  </div>
</template>

<script>
  import TheLoginDialog from "./the-login-dialog";
  import Notify from "../util/notify";
  export default {
    name: "the-header",
    components: {TheLoginDialog},
    data() {
      return {
        activeIndex: '',
        loginMember: {}
      };
    },
    mounted() {
      let _this = this
      _this.activeIndex = _this.$route.name
      _this.loginMember = Tool.getLoginMember();
    },
    watch: {
      $route: {
        handler:function(val, oldVal) {
          let _this = this
          _this.$nextTick(function(){  //页面加载完成后执行
            _this.activeIndex = _this.$route.name
          })
        }
      }
    },
    methods: {
      /**
       * 打开登录注册窗口
       */
      openLoginDialog() {
        let _this = this;
        _this.$refs.loginComponent.openLoginDialog();
      },

      setLoginMember(loginMember) {
        let _this = this;
        _this.loginMember = loginMember;
      },

      /**
       * 退出登录
       */
      logout () {
        let _this = this;
        _this.$axios.get(process.env.VUE_APP_SERVER + '/business/web/member/logout/' + _this.loginMember.token).then((response)=>{
          let resp = response.data;
          if (resp.success) {
            Tool.setLoginMember(null);
            _this.loginMember = {};
            Notify.success('退出登录')
            _this.$router.push("/");
          } else {
            Notify.error(resp.message)
          }
        });
      },
    }
  }
</script>

<style lang="less" scoped>
  .the-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    height: 60px;
    padding: 0 220px;
    background-color: white;
    box-shadow: 0 3px 5px rgba(0, 0, 0, .20);
    z-index: 101;
    position: fixed;
    width: 100%;

    /*border-radius: 4px;*/

    .the-header-left {
      display: flex;
      min-width: 700px;

      .the-header-left-logo-title {
        font-size: 20px;
        line-height: 60px;

        .the-header-left-logo-title-span {
          margin-right: 10px;
        }
      }
    }

    .the-header-right {
      display: flex;

      .the-header-right-button {
        font-size: 16px;
      }

      .the-header-right-div {
        line-height: 42px;
        font-size: 16px;
      }
    }
  }

  .el-menu-item {
    font-size: 17px;

    .router-link-active {     //点击时去掉下划线
      text-decoration: none;
    }
  }

  /deep/ .el-submenu__title {
    font-size: 17px;
  }
</style>